package com.example.administrator.myapplication.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.myapplication.R;

/**
 * Time:2019/4/23 0023
 * Author:ycf
 */
public class MyView extends View {

    private final Context context;
    private Paint paint = new Paint();
    private Path path = new Path();
    private float startY, startX;
    private float endX, endY;
    private Bitmap bitmap;
    private Canvas canvasBit;

    public MyView(Context context) {
        super(context);
        this.context = context;
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();

    }

    private void init() {
        paint.setStyle(Paint.Style.STROKE);
//        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(10f);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background).copy(Bitmap.Config.ARGB_8888, true);
        canvasBit = new Canvas(bitmap);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                path.moveTo(event.getX(),event.getY());
                startX = event.getX();
                startY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
//                path.lineTo(event.getX(),event.getY());
//                float endX = (startX + event.getX()) / 2;
//                float endY=(startY+event.getY())/2;
//                path.quadTo(startX,startY,endX,endY);
//                startX=event.getX();//重置结束点为开始点
//                startY=event.getY();

                endX = event.getX();
                endY = event.getY();
                invalidate();

//                postInvalidate();//当不确定是否在主线程时用此方法重绘
//                invalidate();//确定在主线程时用此方法重绘
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        return true;
    }

    @SuppressLint("NewApi")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画贝塞尔二阶曲线
//        path.moveTo(100,300);//贝塞尔曲线起点，若不指定就是远点（0,0）
//        path.quadTo(200,200,300,300);//前两个参数是控制点的xy轴，后两个点终点的xy轴
//        path.quadTo(400,400,500,300);//这条曲线同上，但是起点是上一条的终点
//        path.rQuadTo(100,-100,200,0);
//        path.rQuadTo(100,100,200,0);
//        canvas.drawPath(path,paint);//绘制路径
//        paint.setShadowLayer(10,15,15,Color.GRAY);//阴影效果
//        paint.setTextAlign(Paint.Align.LEFT);//设置文字对齐方式
//        paint.setTextSize(80);

        //设置文字样式
//        paint.setFakeBoldText(true);//粗体
//        paint.setUnderlineText(true);//下划线
//        paint.setTextSkewX((float)-0.25);//斜体 正常斜体-0.25
//        paint.setStrikeThruText(true);//删除线

//        paint.setTextScaleX(2);//只会将水平方向拉伸，高度不会变

//        canvas.drawText("来得及发生了法律上",10,100,paint);//画文字
//        canvas.drawCircle(500,500,100,paint);//画圆
//        canvas.save();
        canvasBit.save();
        RectF rectF = new RectF(startX, startY, endX, endY);
//        canvasBit.drawRoundRect(rectF, 10, 10, paint);
//        canvasBit.restore();
//        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawRoundRect(rectF,10,10,paint);//画带圆角的矩形
//        canvas.restore();
//        canvas.drawOval(50,50,200,240,paint);//画椭圆
    }

}
